<?php

namespace app\beautiful\model\member;

use app\beautiful\model\LevelModel;

use app\beautiful\model\order\GoodsOrderModel;
use app\beautiful\model\PartnerModel;
use app\beautiful\model\StaffMemberModel;

use app\common\model\CommonModel;
use app\stores\model\member\MemberLevelModel;
use think\facade\Db;


class MemberModel extends CommonModel{

    protected  $name = 'beautiful_member';

    protected  $pk   = 'member_id';

    public function getById($where,$field='*')
    {

        return $this->where($where)->field($field)->find();
    }


    public function level()

    {

        return $this->hasOne(LevelModel::class,'level_id','level_id');

    }

    public function memberlevel()

    {

        return $this->hasOne(MemberLevelModel::class,'member_id','member_id');

    }


    public function balance()

    {

        return $this->hasMany(MemberBalanceModel::class,'member_id','member_id');

    }



    public function integral()

    {

        return $this->hasMany(MemberIntegralModel::class,'member_id','member_id');

    }



    public function adviser()

    {

        return $this->hasOne(StaffMemberModel::class,'member_id','member_id');

    }



    public function getBirthdayAttr($value,$data)

    {   

        $my = $data['birthday_year'].'-'.$data['birthday_month'].'-'.$data['birthday_day'];

        return empty( $data['birthday_year']) ? '' : $my;

    }



    public function getHiddenMobileAttr($value,$data)

    {

        $mobile=$data['mobile'] ?? '';

        return yc_phone($mobile);

    }



    public function getMemberInfoByMemberId($memberIds,$type = 0){

        $where = [

            ['member_id','IN',$memberIds],

        ];



        $datas = $this->field('member_id,face,nick_name,mobile')->where($where)->select()->toArray();

        $return = [];

        foreach($datas as $val){

            $return[$val['member_id']] = $val;

        }

        return $return;

    }

    public function order(){
        return $this->hasMany(GoodsOrderModel::class,'member_id','member_id');
    }
    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('beautiful_member')->alias('a')
            ->where(['a.is_delete'=>0])
            ->when(isset($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->when(isset($where['pid']),function ($query) use ($where){
                $query->where(['a.pid'=>$where['pid']]);
            })
            ->when(isset($where['member_ids']),function ($query) use ($where){
                $query->where('a.member_id','In',$where['member_ids']);
            })
            ->when(!empty($where['partner_id']),function ($query) use ($where){
                $query->where(['a.partner_id'=>$where['partner_id']]);
            })
            ->when(!empty($where['member_num']),function ($query) use ($where){
                $query->where(['a.member_num'=>$where['member_num']]);
            })
            ->when(!empty($where['nick_name']),function ($query) use ($where){
                $query->where('a.nick_name','like','%'.$where['nick_name'].'%');
            })
            ->when(!empty($where['mobile']),function ($query) use ($where){
                $query->where('a.mobile','like','%'.$where['mobile'].'%');
            })
            ->when(isset($where['platform_money_type']) && $where['platform_money_type']==1,function ($query) use ($where){
                $query->where('a.money','>',0);
            })
            ->when(isset($where['platform_money_type']) && $where['platform_money_type']==2,function ($query) use ($where){
                $query->where('a.money','=',0);
            })
            ->when(isset($where['lock_type']) && $where['lock_type']==1,function ($query) use ($where){
                $query->where('a.lock_shop_ratio','>',0);
            })
            ->when(isset($where['lock_type']) && $where['lock_type']==2,function ($query) use ($where){
                $query->where('a.lock_shop_ratio','=',0);
            });
        $total_money = $query->sum('a.money');
        $query = $query->field("a.member_id,a.mobile,a.add_time,a.nick_name,a.recharge,a.member_num,a.birthday_year,a.birthday_month,a.birthday_day,a.face,a.lock_shop_ratio,a.status,a.money");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['num'] = $count-(($page-1)*$limit+$k);
                $list[$k]['add_time']=date('Y-m-d H:i:s',$v['add_time']);
                $s=[];
                $s['member_id']=$v['member_id'];
                $list[$k]['lock_shop']=MemberLockModel::list($s)['list'];
            }
        }
        $total_money = sprintf("%.2f",$total_money);

        return compact('count','list','total_money');
    }
    public function partner()

    {

        return $this->hasOne(PartnerModel::class,'member_id','member_id');

    }

    public static function info($where,$field='*'){
        $info = Db::name('beautiful_member')->alias('m')
            ->join('beautiful_member_level ml','m.member_id=ml.member_id')
            ->where(['m.is_delete'=>0,'ml.is_delete'=>0])
            ->when(isset($where['member_id']),function ($query) use ($where){
                $query->where(['m.member_id'=>$where['member_id']]);
            })
            ->when(isset($where['shop_id']),function ($query) use ($where){
                $query->where(['ml.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['mobile']),function ($query) use ($where){
                $query->where('m.mobile', 'like', '%'.$where['mobile'].'%');
            })
            ->when(isset($where['nick_name']),function ($query) use ($where){
                $query->where('m.nick_name', 'like', '%'.$where['nick_name'].'%');
            })
            ->field($field)->find();
        if(!empty($info)){
            if($info['level_id']){
                $levelModel = new LevelModel();
                $level = $levelModel->getById(['shop_id'=>$info['shop_id'],'level_id'=>$info['level_id'],'type'=>$where['type'],'is_delete'=>0],'name');
                $info['level_name'] = $level?$level['name']:'普通会员';
            }else{
                $info['level_name'] = '普通会员';
            }

        }
        return $info;
    }
}

